﻿Imports Mystery
Imports System.Data
Imports Mystery.DbConnection
Imports System.Data.SqlClient

Public Interface IContentSelectionControler
    Property content As IContent
    Function getContents(input As String) As IEnumerable(Of LightContentReferece)
End Interface

Public Interface IContentSelectionControl
    Property controler As IContentSelectionControler
    Event contentSelected(content As IContent)
End Interface

Public Class ContentSelectionControlerAttr
    Inherits MysteryPropertyAttribute

    Public Property implementing_type As Type

    Private _activator As IActivator

    Dim _generic As Type = GetType(ContentSelectionControler(Of ))

    Public Overrides Sub setUp()

        Dim cd As PropertyControlDefaults = getGlobalObject(Of PropertyControlDefaults)()

        If implementing_type IsNot Nothing Then
            implementing_type.GetInterfaceMap(GetType(IContentSelectionControler))
        Else
            Dim property_type As Type = used_in.PropertyType.GetGenericArguments.FirstOrDefault()

            implementing_type = _generic.MakeGenericType(property_type)
        End If

        _activator = getGlobalObject(Of FastActivator).getActivator(implementing_type)
    End Sub

    Public Function createControler(content As IContent) As IContentSelectionControler
        Dim result As IContentSelectionControler = _activator.createInstance()
        result.content = content
        Return result
    End Function

End Class

Public Class ContentSelectionControler(Of T As IContent)
    Implements IContentSelectionControler

    Private _contents As New List(Of LightContentReferece)
    Dim reference_name As String = GetType(ReferenceText).Name


    Public Sub New()
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        _contents = cd.getLightContentRereferece(Of T)()
    End Sub


    Public Function getContents(input As String) As IEnumerable(Of LightContentReferece) Implements IContentSelectionControler.getContents
        If String.IsNullOrEmpty(input) Then Return _contents
        input = input.ToLower
        Dim result As ICollection(Of LightContentReferece) = New LinkedList(Of LightContentReferece)
        For Each row As LightContentReferece In _contents
            If Not row.ref_text.ToLower.Contains(input) Then Continue For
            result.Add(row)
        Next
        Return result
    End Function

    Public Property content As Mystery.IContent Implements IContentSelectionControler.content

End Class
